package com.online.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.online.entity.ApiResult;
import com.online.entity.ExamManage;
import com.online.service.ExamManageService;
import com.online.util.ApiResultHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 学科考试
 */
@RestController
@Slf4j
public class ExamManageController {

    @Autowired
    private ExamManageService examManageService;

    @GetMapping("/exams/{page}/{size}")
    public ApiResult findAll(@PathVariable("page") Integer page,@PathVariable("size") Integer size){
        log.info(("分页查询所有试卷"));
        ApiResult apiResult;
        Page<ExamManage> examManagePage=new Page<>(page,size);
        IPage<ExamManage> all=examManageService.findAll(examManagePage);
        return ApiResultHandler.buildApiResult(200,"查询成功",all);
    }

    @GetMapping("/exam/{examCode}")
    public ApiResult findById(@PathVariable("examCode") Integer examCode){
        log.info("根据id查找");
        ExamManage res=examManageService.findById(examCode);
        if (res==null){
            return ApiResultHandler.buildApiResult(1000,"考试编号不存在",null);
        }else{
            return ApiResultHandler.buildApiResult(200,"查找成功",res);
        }
    }
    @DeleteMapping("/exam/{examCode}")
    public ApiResult deleteById(@PathVariable("examCode") Integer examCode ){
        int res = examManageService.deleteById(examCode);
        return ApiResultHandler.buildApiResult(200,"删除成功",res);
    }
    @PutMapping("/exam")
    public ApiResult update(@RequestBody ExamManage examManage){
        int res = examManageService.update(examManage);
        log.info("更新考试信息");
        return ApiResultHandler.buildApiResult(200,"更新成功",res);
    }
    @PostMapping("/exam")
    public ApiResult add(@RequestBody ExamManage examManage){
        int res = examManageService.add(examManage);
        if (res ==1) {
            return ApiResultHandler.buildApiResult(200, "添加成功", res);
        } else {
            return  ApiResultHandler.buildApiResult(400,"添加失败",res);
        }
    }
    @GetMapping("/examMangePaperId")
    public ApiResult findOnlyPaperId(){
        ExamManage res = examManageService.findOnlyPaperId();
        if (res != null) {
            return ApiResultHandler.buildApiResult(200,"请求成功",res);
        }
        return ApiResultHandler.buildApiResult(400,"请求失败",res);
    }

}
